﻿using Communication.Modbus;
using STMS.BLL;
using STMS.WSTMSystem.FModels;
using STMS.WSTMSystem.UControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.WSTMSystem.StoreTemper
{
    public partial class FrmSetSRegionTemper : Form
    {
        public FrmSetSRegionTemper()
        {
            InitializeComponent();
        }

        RegionTemperBLL rtBLL = new RegionTemperBLL();
        public event Action<UCBoxArgs> ReloadRegion;//刷新调整温度后的分区状态
        RegionTemperData fModel = null;
        int isComplete = -1;
        private void FrmSetSRegionTemper1_Load(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                fModel = this.Tag as RegionTemperData;
                if (fModel != null)
                {
                    if (fModel.TemperState == 0)
                    {
                        lblWayName.Text = "升温";
                    }
                    else
                    {
                        lblWayName.Text = "降温";
                    }
                    InitSetWays();//加载升温或降温方式

                }
            }
        }

        private void InitSetWays()
        {
            chklbList.Items.Clear();
            if (fModel.TemperState == 0)
            {
                chklbList.Items.Add("打开热风！");
                chklbList.Items.Add("打开暖气！");
                chklbList.Items.Add("关闭窗户！");
            }
            else
            {
                chklbList.Items.Add("打开冷风！");
                chklbList.Items.Add("关闭暖气！");
                chklbList.Items.Add("打开窗户！");
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            isComplete = 0;
            string msgTitle = "调整分区温度";
            decimal srTemper = txtTemperature.Value;
            if (fModel.TemperState == 0 && fModel.SRTemper >= srTemper)
            {
                MsgBoxHelper.MsgErrorShow(msgTitle, "当前温度需要升温，目标温度低于当前室温！");
                return;
            }
            else if (fModel.TemperState == 2 && fModel.SRTemper <= srTemper)
            {
                MsgBoxHelper.MsgErrorShow(msgTitle, "当前温度需要降温，目标温度高于当前室温！");
                return;
            }
            string updowType = "";
            if (fModel.TemperState == 0)
                updowType = "升温";
            else
                updowType = "降温";
            if (chklbList.CheckedItems.Count == 0)
            {
                MsgBoxHelper.MsgErrorShow(msgTitle, $"请设置{updowType}方式！");
                return;
            }
            Task.Run(() =>
            {
                this.Invoke(new Action(() => {
                    listMsg.Items.Add($"开始{updowType}处理！");
                }));
                Thread.Sleep(500);
                this.Invoke(new Action(() => {
                    listMsg.Items.Add($"{updowType}。。。");
                }));
                Thread.Sleep(5000);
                //修改分区当前室内为目标温度
                SetTemperature(fModel.SId, srTemper);

                bool blUpdate = rtBLL.UpdateRegionTemperature(fModel.RegionId, srTemper);
                if (blUpdate)
                {
                    isComplete = 1;
                    this.Invoke(new Action(() => {
                        UCBoxArgs arg = new UCBoxArgs(fModel.RegionId, 1, srTemper, fModel.SId);
                        ReloadRegion?.Invoke(arg);
                        listMsg.Items.Add($"{updowType}工作已完成！2秒后关闭设置页面");
                    }));
                    Thread.Sleep(2000);
                    this.Invoke(new Action(() => {
                        this.Close();
                    }));

                }
            });

        }

        /// <summary>
        /// 修改温度
        /// </summary>
        /// <param name="sid"></param>
        /// <param name="srTemper"></param>
        private void SetTemperature(int sid, decimal srTemper)
        {
            ModbusBase mb = new ModbusRTU("COM1", 9600, 8, StopBits.One, Parity.None);
            if (mb.Open())
            {
                bool rebl = Task.Run(async () =>
                {
                    ushort val = (ushort)(srTemper * 10);
                    bool bl = await mb.WriteUInt16(sid, val);
                    return bl;
                }).Result;
                mb.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (isComplete == 1 || isComplete == -1)
                this.Close();
            else
            {
                MsgBoxHelper.MsgErrorShow("调整温度", "当前正在调整处理中，不能关闭！");
                return;
            }
        }
    }
}
